<?php
namespace app\api\controller;

use app\api\logic\OrderLogic;
use app\common\model\Client_;
use app\common\server\ConfigServer;
use app\common\server\WechatMiniExpressSendSyncServer;
use think\Db;

/**
 * 订单
 * Class Order
 * @package app\api\controller
 */
class OrderController extends ApiBase
{
    //订单列表
    public function lists()
    {
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $type = $this->request->get('type', 'all');
        $order_list = OrderLogic::getOrderList($user_id, $type,$this->page,$this->limit);
        return success($order_list);
    }

    //下单接口
    public function buy()
    {
        $post = $this->request->post();
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $post['client'] =1;
        $check = $this->validate($post, 'app\api\validate\Order.buy');
        if (true !== $check) {
            return error($check);
        }
        $action = $post['action'];
        $info = OrderLogic::info($post, $user_id);
        
        if ($info['code'] == 0) {
           
            return error($info['msg']);
        }
        
        if ($action == 'info') {
            return success($info['data']);
        }
        
        $order = OrderLogic::add($user_id, $info['data'], $post);
        
        return $order;
    }


    //订单详情
    public function detail()
    {
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $order_id = $this->request->get('id');
        if (!$order_id){
            return error('请选择订单');
        }
        $order_detail = OrderLogic::getOrderDetail($order_id, $user_id);
        if (!$order_detail) {
            return error('订单不存在了!', '');
        }
        return success($order_detail,'获取成功');
    }
    
    /**
     * @notes 微信确认收货 获取详情
     * @return \think\response\Json
     * @author lbzy
     * @datetime 2023-09-05 09:51:41
     */
    function wxReceiveDetail()
    {
        return success('获取成功', OrderLogic::wxReceiveDetail(input('order_id/d'), $this->user_id));
    }

    //取消订单
    public function cancel()
    {
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $order_id = $this->request->post('order_id');
        $cancel_description = $this->request->post('cancel_description') ?? '';
        if (empty($order_id)) {
            return error('参数错误');
        }
        
        return OrderLogic::cancel($order_id, $user_id, $cancel_description);
    }


    //删除订单
    public function del()
    {
        $order_id = $this->request->post('id');
        if (empty($order_id)) {
            return error('参数错误');
        }
        return OrderLogic::del($order_id, $this->user_id);
    }


    //确认订单
    public function confirm()
    {
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $order_id = $this->request->post('order_id');
        if (empty($order_id)) {
            return error('参数错误');
        }
        return OrderLogic::confirm($order_id, $user_id);
    }


    public function orderTraces()
    {
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $order_id = $this->request->post('id');
        $tips = '参数错误';
        if ($order_id) {
            $traces = OrderLogic::orderTraces($order_id, $user_id);
            if ($traces) {
                return success($traces,'获取成功');
            }
            $tips = '暂无物流信息';
        }
        return error($tips);
    }


    /**
     * @notes 核销订单列表
     * @author ljj
     * @date 2021/8/18 3:58 下午
     */
    public function verificationLists()
    {
        $type = $this->request->get('type',\app\common\model\Order::NOT_WRITTEN_OFF);
        $lists = OrderLogic::verificationLists($type, $this->user_id, $this->page_no, $this->page_size);
        return success('获取成功', $lists);
    }


    /**
     * @notes 提货核销
     * @author ljj
     * @date 2021/8/18 4:36 下午
     */
    public function verification()
    {
        $post = $this->request->post();
        $user_id =$this->uid??0;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $post['user_id'] = $user_id;
        $check = $this->validate($post, 'app\api\validate\Order.verification');
        if (true !== $check) {
            return error($check);
        }
        $result = OrderLogic::verification($post);
       return success('获取成功',$result);
    }

    /**
     * @notes 确认提货
     * @author ljj
     * @date 2021/8/18 7:02 下午
     */
    public function verificationConfirm()
    {
        $post = $this->request->post();
        $post['user_id'] = $this->uid??0;
        $result = OrderLogic::verificationConfirm($post);
        if (true !== $result) {
            return error($result);
        }
        return success('获取成功');
    }

    /**
     * @notes 获取配送方式
     * @author ljj
     * @date 2021/8/19 7:17 下午
     */
    public function getDeliveryType()
    {
        $data = OrderLogic::getDeliveryType();
        return success('获取成功', $data);
    }
    
    /**
     * @notes 微信同步发货 查询
     * @return \think\response\Json
     * @author lbzy
     * @datetime 2023-09-07 15:27:17
     */
    function wechatSyncCheck()
    {
        $id     = $this->request->get('id');
        
        $order  = \app\common\model\Order::where('id', $id)->where('user_id', $this->user_id)->findOrEmpty();
        
        $result = WechatMiniExpressSendSyncServer::wechatSyncCheck($order);
        
        if (! $result) {
            return error('查询失败');
        }
    
        return success('查询成功', $result);
    }
}